﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Reflection;
using Microsoft.VisualStudio.Shell;
using PS.WSSDeploy.Client;
using PS_WSSDeploy_Package.Application;
using PS_WSSDeploy_Package.Utils;

namespace PS_WSSDeploy_Package.WdCommand
{
    class CopyThisToRootCmd : BaseCmd, IWdTask
    {
        public static BaseCmd Create(OleMenuCommandService service, bool enableForSandbox)
        {
            var instance = new CopyThisToRootCmd();
            var cmdId = new CommandID(GuidList.guidPS_WSSDeploy_PackageCmdSet, (int)PkgCmdIDList.cmdidCopyThisToRootCommand);
            AddCommandToMenu(service, instance, cmdId, enableForSandbox);
            return instance;
        }

        public override void DoExecute()
        {
            AddTask(this);
        }

        public void TaskExecute()
        {
            var selectedItems = SelectedWdProjectItems;

            try
            {
                if (selectedItems.Count == 0)
                {
                    throw new ArgumentException(WssDeployResources.CopyThisToRootCmd_ProjectIsNotSelected);
                }

                var sharePointRootFiles = SharePointProjectManager.GetRootFilesForWdItems(selectedItems);

                if (sharePointRootFiles.Count > 0)
                {
                    //for SharePoint Project Mapped Items
                    SharePointProjectManager.ReplaceToken(SpFileToken.ProjectAssemblyFullName, ProjectAssemblyFullName, sharePointRootFiles);
                    //Client.CopyToRoot(sharePointRootFiles);
                    Cc.CopyToRoot(sharePointRootFiles);
                }
                else
                {
                    //for Old Type Project with '12' or '14' folders
                    var itemIsLocatedAtRootFolder = true;
                    foreach (var item in selectedItems)
                    {
                        if (!IsLocatedAtRootFolder(item.FullPath))
                        {
                            itemIsLocatedAtRootFolder = false;
                            break;
                        }
                    }
                    if (!itemIsLocatedAtRootFolder)
                    {
                        throw new ApplicationException(WssDeployResources.CopyThisToRootCmd_ProjectIsNotLocatedInSharePointRoot);
                    }

                    var rootFiles = new List<RootFile>();
                    var rootFolders = new List<string>();
                    foreach (var item in selectedItems)
                    {
                        if (item.Type == WdProjectItemType.File)
                        {
                            rootFiles.Add(GetRootFile(item.FullPath));
                        }
                        if (item.Type == WdProjectItemType.Folder)
                        {
                            rootFolders.Add(item.FullPath);
                        }
                    }
                    if (rootFiles.Count > 0)
                    {
                        //Client.CopyToRoot(rootFiles);
                        Cc.CopyToRoot(rootFiles);
                    }
                    else if (rootFolders.Count > 0)
                    {
                        //only for first selected
                        //Client.CopyToRoot(RootFolder, rootFolders[0]);
                        Cc.CopyToRoot(RootFolder, rootFolders[0]);
                    }
                }

                WriteOutput(string.Format(WssDeployResources.CopyThisToRootCmd_CopyToRootItemSucceeded, selectedItems.Count));
                SetSuccess();
            }
            catch (Exception ex)
            {
                WriteOutput(string.Format(WssDeployResources.CopyThisToRootCmd_CopyToRootItemError, selectedItems.Count, ex.Message));
                SetFailed();
            }
        }
    }
}
